import { def } from './utils'
import { defineReactive } from './defineReactive'
import { arrayMethods } from './array'
import { observe } from './observe'
import Dep from './Dep'
export default class Observer {
  constructor(val) {
    this.dep = new Dep()
    def(val, '__ob__', this, false) // 把val身上添加一个Observer实例对象 值为__ob__
    if (Array.isArray(val)) {
      Object.setPrototypeOf(val, arrayMethods)
      this.observeArray(val)
    } else {
      this.walk(val)
    }
  }
  walk(val) {
    for (const i in val) {
      defineReactive(val, i)
    }
  }
  observeArray(arr) {
    for (let i = 0; i < arr.length; i++) {
      observe(arr[i])
    }
  }
}
